﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PFMConfigurations;
using PFMCandidate;
using PFMHandlers;
using System.IO;
using PFMKernels;

namespace PFMApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        PFMConfigurationManager PFMcm = new PFMConfigurationManager();
        int Index = 0;
        public MainWindow()
        {
           //a.(PFMcm.Targets[0]);

            
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
            选择路径 a = new 选择路径();
            原始路径.Text= a.OFD();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            选择路径 a = new 选择路径();
            目标路径.Text = a.SFD();
        }
        private void 清空()
        {
            原始路径.Text = "";
            目标路径.Text = "";
            名称.Text = "";
            搜索类型.Text = "";
            根目录.Text = "";
            是否扫描子目录.Text ="";
            保存目录.Text = "";
            Handler1.Text = "";
            Handler2.Text = "";
        }
        private void 异常处理()
        {
            if (目标路径.Text == "" | 原始路径.Text == "" || 名称.Text == "")
            {
                MessageBox.Show("路径为空或名称为空");
            }
        }
        private void 隐藏or显示()
        {
            if(配置.Visibility==Visibility.Hidden)
            {
                目标路径.Visibility = Visibility.Hidden;
                原始路径.Visibility = Visibility.Hidden;
                目标路径按钮.Visibility = Visibility.Hidden;
                原始路径按钮.Visibility = Visibility.Hidden;
                名称Label.Visibility = Visibility.Hidden;
                名称.Visibility = Visibility.Hidden;
                加密.Visibility = Visibility.Hidden;
                解密.Visibility = Visibility.Hidden;
                压缩.Visibility = Visibility.Hidden;
                配置.Visibility = Visibility.Visible;
                配置文件.Content = "返回";
            }
            else
            {
                目标路径.Visibility = Visibility.Visible;
                原始路径.Visibility = Visibility.Visible;
                目标路径按钮.Visibility = Visibility.Visible;
                原始路径按钮.Visibility = Visibility.Visible;
                名称Label.Visibility = Visibility.Visible;
                名称.Visibility = Visibility.Visible;
                加密.Visibility = Visibility.Visible;
                解密.Visibility = Visibility.Visible;
                压缩.Visibility = Visibility.Visible;
                配置.Visibility = Visibility.Hidden;
                配置文件.Content = "配置文件";
            }
        }
        private void Is只读()
        {
            if (根目录.IsReadOnly == true)
            {
                根目录.IsReadOnly = false;
                是否扫描子目录.IsReadOnly = false;
                保存目录.IsReadOnly = false;
                Handler1.IsReadOnly = false;
                Handler2.IsReadOnly = false;
            }
            else
            {
                根目录.IsReadOnly = true;
                是否扫描子目录.IsReadOnly = true;
                保存目录.IsReadOnly = true;
                Handler1.IsReadOnly = true;
                Handler2.IsReadOnly = true;
            }
        }
        private void 显示xml文档()
        {
            搜索类型.Text = PFMcm.Targets[Index].Filter;
            根目录.Text = PFMcm.Targets[Index].Location;
            是否扫描子目录.Text = PFMcm.Targets[Index].Subdirectory.ToString(); ;
            保存目录.Text = PFMcm.Targets[Index].Dir;
            if(PFMcm.Targets[Index].Handler.Count()>1)
            {
                Handler1.Text = PFMcm.Targets[Index].Handler[0];
                Handler2.Text = PFMcm.Targets[Index].Handler[1];
            }
            else
            {
                Handler1.Text = PFMcm.Targets[Index].Handler[0];
                Handler2.Text = "";
            }
           
        }
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Candidate aCandidate = CandidateFactory.CreateCandidate(原始路径.Text);
            aCandidate.Target.Dir = 目标路径.Text + @"\" + 名称.Text + ".zip";
            byte[] baTarget = null;
            PFMHandler Zip = PFMHandlerFactory.CreateHandler("ZIP");
            Zip.Perform(aCandidate, baTarget);
            if(File.Exists(aCandidate.Target.Dir))
            {
                MessageBox.Show("压缩成功");
                清空();
            }
        }

        private void 加密_Click(object sender, RoutedEventArgs e)
        {
            异常处理();
            Candidate aCandidate = CandidateFactory.CreateCandidate(原始路径.Text);
            aCandidate.Target.Dir = 目标路径.Text + @"\" + 名称.Text + ".txt";
            byte[] fileByteArray = null;
            PFMFileHandler sg = new PFMFileHandler();
            PFMEncodeHandler Encode = new PFMEncodeHandler();
            sg.Perform(aCandidate, Encode.PFMEncode(sg.Perform(aCandidate, fileByteArray)));
            if(File.Exists(aCandidate.Target.Dir))
            {
                MessageBox.Show("加密成功");
                清空();
            }

        }

        private void 解密_Click(object sender, RoutedEventArgs e)
        {
            Candidate aCandidate = CandidateFactory.CreateCandidate(原始路径.Text);
            aCandidate.Target.Dir = 目标路径.Text + @"\" + 名称.Text + ".txt";
            byte[] fileByteArray1 = null;
            PFMFileHandler sg = new PFMFileHandler();
            PFMDecodeHandler Decode = new PFMDecodeHandler();
            sg.Perform(aCandidate, Decode.PFMDecode(sg.Perform(aCandidate, fileByteArray1)));
            if (File.Exists(aCandidate.Target.Dir))
            {
                MessageBox.Show("解密成功");
                清空();
            }
        }

        private void 配置文件_Click(object sender, RoutedEventArgs e)
        {
            隐藏or显示();

        }

        private void 显示XML文档_Click(object sender, RoutedEventArgs e)
        {

            显示xml文档();
            下一个.IsEnabled = true;
            上一个.IsEnabled = true;
        }

        private void 修改_Click(object sender, RoutedEventArgs e)
        {
            Is只读();
        }

        private void 保存_Click(object sender, RoutedEventArgs e)
        {
            Is只读();
            PFMcm.Targets[Index].Location = 根目录.Text;
            if (是否扫描子目录.Text.ToUpper() == "TRUE")
            {
                PFMcm.Targets[Index].Subdirectory = true;
            }
            else
            {
                PFMcm.Targets[Index].Subdirectory = false;
            }

            PFMcm.Targets[Index].Dir = 保存目录.Text;
             PFMcm.Targets[Index].Handler[0]=Handler1.Text ;
             PFMcm.Targets[Index].Handler[1] = Handler2.Text;
             PFMcm.SaveTarget();
             MessageBox.Show("保存成功");
             清空();
        }

        private void 上一个_Click(object sender, RoutedEventArgs e)
        {
            if (Index!=0)
            {
                Index--;
                显示xml文档();
            }
            else if (Index ==0)
            {
                MessageBox.Show("到达第一条");
            }
                
        }

        private void 下一个_Click(object sender, RoutedEventArgs e)
        {
            if (Index < PFMcm.Targets.Count()-1)
            {
                Index++;
                显示xml文档();
                
            }
            else if(Index==PFMcm.Targets.Count()-1)
            {
                MessageBox.Show("到达最后一条");
            }
                
        }

        private void 执行_Click(object sender, RoutedEventArgs e)
        {
            PFMCoordinator pfccoor = new PFMCoordinator();
            pfccoor.DoSpecifiedTask(pfccoor.ConfigurationsManager.Targets[Index]);
            if( Directory.GetFiles(@"D:\HomeWork\保存数据").Count()>0)
            {
                MessageBox.Show("保存成功");
            }
        }

    }
}
